﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CacheService.Standard;
using DAL.Base;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Admin
{
    /// <summary>
    /// 开始
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        /// <summary>
        /// Gets the configuration.
        /// </summary>
        /// <value>The configuration.</value>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 此方法由运行时调用。使用此方法向容器添加服务。
        /// </summary>
        /// <param name="services"></param>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //过滤器
            services.AddMvc(c => {
                c.Filters.Add(typeof(AOP.Filter.Login.LoginFilterAttribute));
                c.Filters.Add(typeof(AOP.Filter.Jurisdiction.JurisdictionFilterAttribute));
                c.Filters.Add(typeof(AOP.Filter.Exception.AopExceptionFilterAttribute));
                c.Filters.Add(typeof(AOP.Filter.Result.AopResultFilterAttribute));
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.Configure<CookiePolicyOptions>(options =>
            {
                // 这里是否要经过用户同意 
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            //数据库配置
            services.AddDbContext<ContextBase>(c => c.UseSqlServer(Configuration.GetConnectionString("database")));
            //所有接口注入
            services.AutoFacInterface();
            //缓存配置
            var cacheStr = Configuration.GetConnectionString("cache");
            if (string.IsNullOrWhiteSpace(cacheStr))
            {
                services.AddSingleton<ICache, MemoryForCache>();
            }
            else
            {
                services.AddSingleton(typeof(ICache), new RedisForCache(new RedisCacheOptions
                {
                    Configuration = cacheStr,
                    InstanceName = "api_name"
                }, 0));
            }
            //微信API配置
            services.UseWeChatApi(Configuration.GetSection("WeChat"));
            //微信支付配置
            services.UseWeChatPay(Configuration.GetSection("Pay"));
            //小程序配置
            services.UseWeChatApp(Configuration.GetSection("SmallApp"));
            return services.BuildServiceProvider();
        }

        /// <summary>
        /// 此方法由运行时调用。使用此方法配置HTTP请求管道。
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,SERVICE.Admin.IService.ISyetemService syetemService)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            syetemService.InitData();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
